package org.jrpc.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import org.jrpc.base.IClient;
import org.jrpc.base.ResultResponse;
import org.jrpc.common.baen.RpcRequest;
import org.jrpc.common.baen.RpcResponse;
import org.jrpc.common.codec.MsgPackDecode;
import org.jrpc.common.codec.MsgPackEncode;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by Administrator on 2017/8/28.
 */
@Deprecated
public class Client extends Thread implements IClient {

    private int SERVER_PORT=9080;
    private String SERVER_HOST="127.0.0.1";

    private boolean sign=true;
    private LinkedBlockingQueue<RpcRequest> queue=new LinkedBlockingQueue<>();
    private ConcurrentHashMap<String,RpcResponse> map=new ConcurrentHashMap<>();
    private ClientHandler clientHandler=new ClientHandler(map);
    private EventLoopGroup workerGroup = new NioEventLoopGroup();
    public static void main(String[] args) {
        Client client = new Client();
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        client.put(new RpcRequest(UUID.randomUUID().toString()));

    }

    @Override
    public ResultResponse put(RpcRequest request){
        ResultResponse reResponse=null;
        try {
            reResponse=new ResultResponse(clientHandler.sendMsg(request));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return reResponse;
    }


    @Override
    public RpcResponse init(EventLoopGroup eventLoop) throws Exception {

        try {
            Bootstrap b = new Bootstrap();
            b.group(workerGroup);
            b.channel(NioSocketChannel.class);
            b.option(ChannelOption.SO_KEEPALIVE, true);
            b.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(65536, 0, 2, 0, 2));
                    ch.pipeline().addLast(new MsgPackDecode());
                    ch.pipeline().addLast(new LengthFieldPrepender(2));
                    ch.pipeline().addLast(new MsgPackEncode());
                    ch.pipeline().addLast(clientHandler);
                    System.out.println();
                }
            });
            ChannelFuture f = b.connect(SERVER_HOST, SERVER_PORT).sync();
            f.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            workerGroup.shutdownGracefully();
        }
        return null;
    }

    @Override
    public Bootstrap createBootstrap(Bootstrap bootstrap, EventLoopGroup eventLoop) {
        return null;
    }

    public Client(){
        this.start();
//        new Task().start();
    }


    @Override
    public void run() {
        try {
            init(workerGroup);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class Task extends Thread{
        @Override
        public void run() {
            while (sign){
                try {
                    clientHandler.sendMsg(queue.take());
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
